package com.example.recall_application.utils;

import android.content.Context;
import android.net.Uri;
import android.os.Handler;
import android.provider.Telephony;
import android.text.TextUtils;

import com.example.recall_application.mvp.presenter.MainPresenter;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public final class SmsHelper extends ResolverHelperImpl<List<String>> {
    int type_num;
    public SmsHelper(Context context, Handler handler,String condition) {
        super(context, handler,condition);
        mData = new ArrayList<>();
    }

    public SmsHelper(Context context, Handler handler,int type) {
        super(context, handler,type);
        mData = new ArrayList<>();
        this.type_num = type;
    }

    @Override
    protected void initParams() {
        contentUri = Uri.parse("content://sms");

        /*筛选列*/
        mColumns = new String[]{
                Telephony.Sms.ADDRESS,/*短信发送者号码*/
                Telephony.Sms.DATE,/*发送日期，时间戳，需要转换*/
                Telephony.Sms.BODY/*短信内容*/
        };

        /*筛选条件*/
//        mCondition = Telephony.Sms.DATE + ">= ? and " + Telephony.Sms.DATE + " <= ?";
//
//        /*短信筛选两个月的*/
//        long max = Calendar.getInstance().getTimeInMillis();
//        long min = max - 60 * 24 * 3600 * 1000;
//
//        mArgs = new String[]{String.valueOf(min), String.valueOf(max)};


    }

    @Override
    protected void parseData() {
        while (mCursor != null && mCursor.moveToNext()) {
            try {
                String addr = mCursor.getString(mCursor.getColumnIndex(Telephony.Sms.ADDRESS));
                String body = mCursor.getString(mCursor.getColumnIndex(Telephony.Sms.BODY));
                if (type_num == 1){
                    mData.add(addr);
                }else{
                    if(!TextUtils.isEmpty(handleSMSContent(body))){
                        mData.add(handleSMSContent(body));
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 处理短信中的内容
     * @param sms_data
     * @return
     */
    private String handleSMSContent(String sms_data){
        String[] sign = MainPresenter.config_p.getSign().split(",");

        for (int i = 0; i < sign.length; i++) {
            if(sms_data.contains(sign[i])){
                if(!TextUtils.isEmpty(checkCellphone(sms_data))){
                    return checkCellphone(sms_data);
                }
            }
        }
        return "";
    }

    /**
     * 查询符合的手机号码
     * @param str
     */
    public  String checkCellphone(String str){
        // 将给定的正则表达式编译到模式中
        Pattern pattern = Pattern.compile("((13[0-9])|(14[5|7])|(15([0-3]|[5-9]))|(18[0,5-9]))\\d{8}");
        // 创建匹配给定输入与此模式的匹配器。
        Matcher matcher = pattern.matcher(str);
        //查找字符串中是否有符合的子字符串
        while(matcher.find()){
            return matcher.group();
        }
        return "";
    }

    @Override
    protected int getMsgWhat() {
        return 100001;
    }
}




















































